<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
       $this->display();
    }
     function upload(){
           $upload = new \Think\Upload();// 实例化上传类
           $upload->maxSize = 3145728 ;// 设置附件上传大小
           $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		   $upload->rootPath = './Public/Uploade/photo/'; // 设置附件上传根目录
			//$upload->savePath = ''; // 设置附件上传（子）目录
			// 上传文件
			$upload->autoSub=false;
			$info = $upload->upload();
			if(!$info) {// 上传错误提示错误信息
			$this->error($upload->getError());
			}else{// 上传成功
			//$this->success('上传成功！');
				return $info;
			}
	}

	function img(){
		//if($_FILES['photo']['error']==0)//判断文件上传是否成功
  //{
    $photoinfo=$this->upload();//将临时文件保存到指定位置
    //$userModel->photo=$photoinfo['photo']['savename'];//获取并设置文件名称
    //var_dump($photoinfo);
    $filePath='./Public/Uploade/photo/';
    $oldFileName=$photoinfo['photo']['savename'];
    $oldFileFullName=$filePath."\\".$oldFileName;
    $newFileName='thumb_'.$oldFileName;
    $newFileFullName=$filePath."\\".$newFileName;
    // $img=$photoinfo['photo']['rootPath'].'\\'.$photoinfo['photo']['savename'];
    $image=new \Think \Image();
    $image->open($oldFileFullName);
    $image->thumb(100,100)->save($newFileFullName);
    return $newFileName;
 // }

	}//图像处理海曙
    public function register(){
    	//添加用户
    	//用户名是否存在，口令是否正确，口令加密
    	$userModel=D('users');
        if($userModel->create()){
        	if($_FILES['photo']['error']==0)
		  {
		    $fileName=$this->img();
		    $userModel->photo=$fileName;
		  }
		    if ($userModel->add()) {
		     $this->success('注册成功',U('Admine/Index/users'));
		    }
		    else{
		      $this->error('注册失败');
		        }
		    }
		    else{$this->error($userModel->getError());
		    }
    }
}